package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController    // @Controller  + @ResponseBody
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    /*传统写法*/
    /*@RequestMapping("/findall")
    @ResponseBody
    public List<Department> findAll(){
        return departmentService.findAll();
    }*/

    /*restful风格*/
    /*查询所有*/
    @GetMapping
    @ApiOperation(value = "部门查询",notes = "查询所有")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /*查询一个*/
    @GetMapping("/{id}")
    @ApiOperation(value = "部门查询",notes = "根据ID查询单个")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }


    /*查询部门树*/
    @GetMapping("/tree")
    @ApiOperation(value = "部门级联查询",notes = "部门级联查询")
    public List<Department> tree(){
        return departmentService.tree();
    }


    /*删除操作   localhost:8080/department/1   @PathVariable 从资源路劲，提取参数
    * AjaxResult  封装数据{}
    * */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "根据ID删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try{
            departmentService.remove(id);
            return AjaxResult.me().setResultObj("fasdfasdf");
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("删除失败");
        }

    }
    /*批量删除*/
    @PatchMapping
    @ApiOperation(value = "部门删除",notes = "根据ID删除")
    public AjaxResult patch(@RequestBody List<Long> ids){
        try{
            departmentService.patch(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("删除失败");
        }

    }


    /*添加
    * sn,name,dirPath,state,manager_id,parent_id
    * 提交json格式的数据{sn:"xx",name:"dd",dirPath:"yy",state:1,manager:{id:1,username:"xxx"},parent:{}}
    *
    * 对象---json【后端----前端】
    * json---对象【前端----后端】
    *
    * mapper
    *       manager.id   parent.id
    * */
    @PutMapping
    @ApiOperation(value = "部门添加",notes = "部门添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        System.out.println(department);
        try{
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("添加失败");
        }

    }
    /*分页接口{totalCount:"",data:[{},{}]}*/
    @PostMapping
    @ApiOperation(value = "部门分页查询",notes = "根据分页条件，查询")
    public PageList<Department> queryForPage(@RequestBody DepartmentQuery query){

        PageList<Department> pageList = departmentService.queryForPage(query);
        return pageList;
    }
}
